package com.aier.user.action;

import java.sql.Timestamp;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.MD5Util;

import org.apache.tools.ant.types.resources.selectors.Date;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.service.IMessageNService;
import com.aier.user.entity.Letter;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("userRegIndexAction")
public class UserRegIndexAction extends UserBaseAction{

    /**
     * 注册页面
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "userService")
    private IUserService userService; 

    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    
    private String account;        //账号(手机号)
    private String pwd;            //密码
    private String message;        //手机验证码
    private String nickname;    //昵称
    private String headimg;//头像
    
    
    @Override
    public String execute(){
        
        return SUCCESS;
    }
    
    //检查手机号是否可用
    public String checkPhone(){
        s = 1;
//        if(UserPhoneUtil.isMobileNO(account)){
        if(userService.searchUserByCondition(account)!=null){
            s = 0;
            error_info = "该手机号已经注册";
        }
        return SUCCESS;
    }
    
   //检查昵称是否可用
    public String checkNickName(){
        s = 1;
        if(userService.countUserByNickName(nickname,null)>0){
            s = 0;
            error_info = "该昵称已经存在";
        }
        return SUCCESS;        
    }
    
    //获取手机随机验证吗
    public String showPhoneMessage(){
        userService.insertPhoneMessage(account);
        s=1;
        return SUCCESS;
    }
    
    public String regUser(){
        s = 1;
        if(userService.checkPhoneMessage(account, message)==0){
            s = 0;
            error_info = "短信验证码有误";
        }else{
            User user = new User(account, MD5Util.getMD5(pwd), nickname, account);
            int ldo=userService.insertUserNormal(user);
            if(ldo==0){
                s = 0;
                error_info = "注册失败";
            }
        }
        return SUCCESS;
    }
    
    public String regUserApp(){
        s = 1;
        
        if(userService.countUserByNickName(nickname,null)>0){
            s = 0;
            error_info = "该昵称已经存在";
            return SUCCESS;
        }
        
        if(userService.checkPhoneMessage(account, message)==0){
            s = 0;
            error_info = "短信验证码有误";
        }else{
            User user = new User(account, MD5Util.getMD5(pwd), nickname, account);
            user.setHeadimg(headimg);
            int ldo=userService.insertUserNormal(user);
            if(ldo==0){
                s = 0;
                error_info = "注册失败";
            }
        }
        return SUCCESS;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

	public void setHeadimg(String headimg) {
		this.headimg = headimg;
	}
    
}